package cn.csg.iotplatform.module.system.service.userzone;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.csg.iotplatform.module.system.controller.admin.userzone.vo.*;
import cn.csg.iotplatform.module.system.dal.dataobject.userzone.UserZoneDO;
import cn.csg.iotplatform.framework.common.pojo.PageResult;
import cn.csg.iotplatform.framework.common.pojo.PageParam;
import cn.csg.iotplatform.framework.common.util.object.BeanUtils;

import cn.csg.iotplatform.module.system.dal.mysql.userzone.UserZoneMapper;

import static cn.csg.iotplatform.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.csg.iotplatform.module.system.enums.ErrorCodeConstants.*;

/**
 * 用户分区关系 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class UserZoneServiceImpl implements UserZoneService {

    @Resource
    private UserZoneMapper userZoneMapper;

    @Override
    public Long createUserZone(UserZoneSaveReqVO createReqVO) {
        // 插入
        UserZoneDO userZone = BeanUtils.toBean(createReqVO, UserZoneDO.class);
        userZoneMapper.insert(userZone);
        // 返回
        return userZone.getId();
    }

    @Override
    public void updateUserZone(UserZoneSaveReqVO updateReqVO) {
        // 校验存在
        validateUserZoneExists(updateReqVO.getId());
        // 更新
        UserZoneDO updateObj = BeanUtils.toBean(updateReqVO, UserZoneDO.class);
        userZoneMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserZone(Long id) {
        // 校验存在
        validateUserZoneExists(id);
        // 删除
        userZoneMapper.deleteById(id);
    }

    private void validateUserZoneExists(Long id) {
        if (userZoneMapper.selectById(id) == null) {
            throw exception(USER_ZONE_NOT_EXISTS);
        }
    }

    @Override
    public UserZoneDO getUserZone(Long id) {
        return userZoneMapper.selectById(id);
    }

    @Override
    public PageResult<UserZoneDO> getUserZonePage(UserZonePageReqVO pageReqVO) {
        return userZoneMapper.selectPage(pageReqVO);
    }

}